package org.batch.config.batch.step;

import org.batch.config.batch.item.reader.TradingJdbcReader;
import org.batch.config.batch.item.writer.TradingCsvWriter;
import org.batch.config.batch.item.writer.TradingJdbcWriter;
import org.batch.config.batch.listener.TradingReaderListener;
import org.batch.config.batch.listener.TradingWriterListener;
import org.springframework.batch.core.Step;
import org.springframework.batch.core.configuration.annotation.StepBuilderFactory;
import org.springframework.batch.item.ItemProcessor;
import org.springframework.batch.item.file.FlatFileItemReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.DependsOn;

import javax.annotation.Resource;

/**
 * 批处理执行步骤配置类
 *
 * @author LiKun
 * @date 2021/9/29 15:53
 */
@Configuration
public class BatchStepConfig {
    private static final int DEFAULT_SKIP_COUNT = 3;
    private static final int DEFAULT_RETRY_COUNT = 3;
    private static final int DEFAULT_CHUNK_COUNT = 1000;
    private static final Class DEFAULT_EXCEPTION = Exception.class;

    @Resource
    private ItemProcessor tradingProcessor;
    @Resource
    private TradingCsvWriter tradingCsvWriter;
    @Resource
    private TradingJdbcReader tradingJdbcReader;
    @Resource
    private TradingJdbcWriter tradingJdbcWriter;
    @Resource
    private FlatFileItemReader flatFileItemReader;
    @Resource
    private TradingReaderListener tradingReaderListener;
    @Resource
    private TradingWriterListener tradingWriterListener;

    @Bean("csvStep")
    @DependsOn({"flatFileItemReader", "tradingReaderListener"})
    public Step csvStep(StepBuilderFactory stepBuilderFactory) {
        return stepBuilderFactory.get("csvStep")
                .chunk(DEFAULT_CHUNK_COUNT)                                       //读取一定量的数据后将其写入到数据库中
                .reader(flatFileItemReader)
                .faultTolerant()
                .retry(DEFAULT_EXCEPTION)
                .retryLimit(DEFAULT_RETRY_COUNT)                                  //数据失败重试次数
                .skip(DEFAULT_EXCEPTION)
                .skipLimit(DEFAULT_SKIP_COUNT)                                    //数据失败的最大条数(超出则会停止Step的运行)
                .listener(tradingReaderListener)
                .processor(tradingProcessor)
                .writer(tradingCsvWriter)
                .faultTolerant()
                .skip(DEFAULT_EXCEPTION)
                .skipLimit(DEFAULT_SKIP_COUNT)                                    //数据失败的最大条数(超出则会停止Step的运行)
                .listener(tradingWriterListener)
                .build();
    }

    @Bean("mysqlStep")
    @DependsOn({"tradingJdbcReader", "tradingReaderListener"})
    public Step mysqlStep(StepBuilderFactory stepBuilderFactory) {
        return stepBuilderFactory.get("mysqlStep")
                .chunk(DEFAULT_CHUNK_COUNT)                                       //读取一定量的数据后将其写入到数据库中
                .reader(tradingJdbcReader)
                .faultTolerant()
                .retry(DEFAULT_EXCEPTION)
                .retryLimit(DEFAULT_RETRY_COUNT)                                  //数据失败重试次数
                .skip(DEFAULT_EXCEPTION)
                .skipLimit(DEFAULT_SKIP_COUNT)                                    //数据失败的最大条数(超出则会停止Step的运行)
                .listener(tradingReaderListener)
                .processor(tradingProcessor)
                .writer(tradingJdbcWriter)
                .faultTolerant()
                .skip(DEFAULT_EXCEPTION)
                .skipLimit(DEFAULT_SKIP_COUNT)                                    //数据失败的最大条数(超出则会停止Step的运行)
                .listener(tradingWriterListener)
                .build();
    }
}
